package com.blog.controller.filter;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OutputReplaceFilter implements Filter {

	private Properties pp = new Properties();

	private static Log log = LogFactory.getLog(OutputReplaceFilter.class); 
	
	public void init(FilterConfig config) throws ServletException {
		System.out.println("OutputReplaceFilter init start ...");
		log.info("-------------------->load OutputReplaceFilter start....!");  
		String file = config.getInitParameter("file");
		String realPath = config.getServletContext().getRealPath(file);
		
		
		System.out.println("####################################"+realPath+"##########################");
		try {
//			pp.load(OutputReplaceFilter.class.getResourceAsStream("character.properties"));
			pp.load(new FileInputStream(realPath));
			
			log.error("-------------------->load "+realPath+" success!");  
		} catch (IOException e) {
		}
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		// 自定义的 response
		HttpCharacterResponseWrapper response = new HttpCharacterResponseWrapper(
				(HttpServletResponse) res);

		// 提交给 Servlet 或者下一个 Filter
		chain.doFilter(req, response);
		
		HttpServletRequest request = (HttpServletRequest) req;
		
		if(!request.getRequestURI().endsWith("vCode.htm")){
			// 得到缓存在自定义 response 中的输出内容
			String output = response.getCharArrayWriter().toString();

			// 修改，替换
			for (Object obj : pp.keySet()) {
				String key = (String) obj;
				output = output.replace(key, pp.getProperty(key));
			}
			// 输出
			PrintWriter out = res.getWriter();
			out.write(output);
			
			out.flush();
			out.close();
		}
		System.out.println(request.getRequestURI());

		
	}

	public void destroy() {
	}

}
